package com.sunq.studio.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.sunq.studio.resource.dao.TbPicResourceDao;
import com.sunq.studio.resource.domain.TbPicResource;
import com.sunq.studio.sys.dao.TbClassDao;
import com.sunq.studio.sys.dao.TbSignClassDao;
import com.sunq.studio.sys.dao.TbSignTeacherDao;
import com.sunq.studio.sys.dao.TbUserDao;
import com.sunq.studio.sys.domain.TbClass;
import com.sunq.studio.sys.domain.TbSignClass;
import com.sunq.studio.sys.domain.TbSignTeacher;
import com.sunq.studio.sys.domain.TbUser;
import com.sunq.studio.sys.service.ClassService;
import com.sunq.studio.sys.vo.ClassSignVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by sunq on 2019/6/30.
 */
@Service
public class ClassServiceImpl implements ClassService {


    Logger logger= LoggerFactory.getLogger(ClassServiceImpl.class);

    @Autowired
    private TbClassDao tbClassDao;

    @Autowired
    private TbSignClassDao tbSignClassDao;

    @Autowired
    private TbUserDao tbUserDao;

    @Autowired
    private TbPicResourceDao tbPicResourceDao;

    @Autowired
    private  TbSignTeacherDao tbSignTeacherDao;

    @Override
    public List<TbClass> getClassAll() {

        return tbClassDao.selectAllClass();
    }

    @Override
    @Transactional
    public int putSignClass(TbSignClass tbSignClass) {

        TbClass classObj= tbClassDao.selectByPrimaryKey(tbSignClass.getClassId());
        TbUser userObj=tbUserDao.selectByPrimaryKey(tbSignClass.getUserId());
        tbSignClass.setSignTime(new Date());
        tbSignClass.setAuditFlag(0);
        tbSignClass.setPrimaryMoney(classObj.getPrice());
        BigDecimal realMoney=classObj.getPrice().multiply(userObj.getCardDiscount()).divide(new BigDecimal(10), 2);

        int i=0;
        if(userObj.getMoney().compareTo(realMoney)>0){
            TbUser userUpdate=new TbUser();
            userUpdate.setId(userObj.getId());
            userUpdate.setMoney(userObj.getMoney().subtract(realMoney));
            tbSignClass.setRealMoney(realMoney);
            tbSignClassDao.insertSelective(tbSignClass);
            i= tbUserDao.updateByPrimaryKeySelective(userUpdate);
        }


        String ss=null;
        ss.indexOf("c");


       return i;
    }


    @Override
    public List<ClassSignVO> getSignedClassList(Date classDate,Integer auditFlag ,int pageNum) {

         PageHelper.startPage(pageNum,20);
        List<ClassSignVO> res=tbSignClassDao.getUnAuditClassSign(auditFlag);

        if(auditFlag==1){
            PageHelper.startPage(pageNum,20);
            List<ClassSignVO> resTeach=tbSignTeacherDao.selectAuditList(auditFlag);


            if(!resTeach.isEmpty()){
                resTeach.stream().forEach(t->t.setIsTeach(1));
                res.addAll(resTeach);
            }
        }



        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
        res.stream().forEach(t->t.setClassDateStr( sdf1.format(t.getClassDate() ) ));

        return res;
    }

    @Override
    public int setClassAuditPass(TbSignClass tbSignClass) {

        logger.info(MessageFormat.format("signClassOk  getId=={0}",tbSignClass.getId() ));

        int i=tbSignClassDao.updateByPrimaryKeySelective(tbSignClass);
        return i;
    }

    @Override
    public int setClassAuditDel(TbSignClass tbSignClass) {

        TbSignClass signClass= tbSignClassDao.selectByPrimaryKey(tbSignClass.getId());


        TbUser user=tbUserDao.selectByPrimaryKey(signClass.getUserId());
        TbUser  userUpdate=new TbUser();


        userUpdate.setId(signClass.getUserId());
        BigDecimal returnMoney=user.getMoney().add(signClass.getRealMoney());
        userUpdate.setMoney(returnMoney);
        tbUserDao.updateByPrimaryKeySelective(userUpdate);
        logger.info(MessageFormat.format("signClassdel  getUserId=={0}signClassgetRealMoney=={1}",signClass.getUserId(), returnMoney.doubleValue()));


        int i=tbSignClassDao.updateByPrimaryKeySelective(tbSignClass);
        return i;
    }

    /**
     * 用户自己的打卡记录
     * @return
     */
    @Override
    public List<ClassSignVO> getUserClassSign( Integer userId,int page) {

        PageHelper.startPage(page,20);
        List<ClassSignVO> res=tbSignClassDao.getUserClassSign(userId);


        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
        res.stream().forEach(t->t.setClassDateStr( sdf1.format(t.getClassDate() ) ));
        return  res;
    }


    @Override
    public int addClass(TbClass tbClass){

        tbClass.setCreateTime(new Date()) ;
        int i= tbClassDao.insertSelective(tbClass) ;
        Integer res=tbClassDao.selectMAXClass(tbClass.getPrice());

        return res;
    }
    @Override
    public int updateClass(TbClass tbClass){

        int i= tbClassDao.updateByPrimaryKeySelective(tbClass);

        return i;
    }
    @Override
    public TbClass  getClassbyId( Integer id) {


        TbClass res=tbClassDao.selectByPrimaryKey(id);

        List<TbPicResource> list=tbPicResourceDao.selectPicsByResId(res.getId(),"class");

        if(!list.isEmpty()){
          //  res.setImages( list.stream().map(t->t.getPicUrl()).collect(Collectors.toList()));
            res.setImages(list);
        }

        return  res;
    }

    @Override
    public int putTeachSignClass(TbSignTeacher tbSignClass) {

            tbSignClass.setSignTime(new Date());
            int i=tbSignTeacherDao.insertSelective( tbSignClass);

        return i;
    }

    @Override
    public List<ClassSignVO> getUnauditList( int page) {

        PageHelper.startPage(page,20);
        List<ClassSignVO> res =tbSignTeacherDao.selectAuditList(0);

        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
        res.stream().forEach(t->t.setClassDateStr( sdf1.format(t.getClassDate() ) ));
        return res;
    }

    @Override
    public List<ClassSignVO> getAuditedList(int page) {

        PageHelper.startPage(page,20);
        List<ClassSignVO> res=tbSignTeacherDao.selectAuditList(1);
        SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
        res.stream().forEach(t->t.setClassDateStr( sdf1.format(t.getClassDate() ) ));

        return res;
    }

    @Override
    public int setTeachClassAudit(TbSignTeacher tbSignClass) {

        logger.info("setTeachClassAudit  getUserId==[{}] delflag=[{}]",tbSignClass.getId() ,tbSignClass.getAuditFlag()  );

        int i=tbSignTeacherDao.updateByPrimaryKeySelective(tbSignClass);

        return i;
    }
}
